package com.yunsc.shop.controller.product;

import com.yunsc.common.base.BaseContext;
import com.yunsc.common.utils.PageResult;
import com.yunsc.common.utils.Result;
import com.yunsc.common.utils.SecurityUtil;
import com.yunsc.common.validator.ValidationGroups;
import com.yunsc.order.dto.OrderCommentDTO;
import com.yunsc.order.sevice.YunscOrderService;
import com.yunsc.product.dto.QueryCommentDTO;
import com.yunsc.product.service.YunscCommentService;
import com.yunsc.product.vo.CommentTotalVO;
import com.yunsc.product.vo.GoodsCommentVO;
import com.yunsc.shop.utils.ClientInfoUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


/**
 * 商品评价记录表
 *
 * @author adan
 * @email 2028038953@gmail.com
 * @date 2023-12-19 02:17:44
 */
@Api(tags = "商品信息管理", value = "商品评价记录接口")
@RestController
@RequestMapping("comment")
public class CommentController {
    @Resource
    private YunscCommentService commentService;
    @Resource
    private YunscOrderService orderService;
    @Resource
    private ClientInfoUtils clientInfoUtils;

    /**
     * 列表
     */
    @ApiOperation(value = "商品评价记录列表", notes = "商品评价记录列表")
    @GetMapping("/listRows")
    public Result<?> queryGoodsCommentList(QueryCommentDTO params) {
        Integer currentStoreId = clientInfoUtils.getCurrentStoreId();
        PageResult<GoodsCommentVO> commentPage = commentService.queryCommentList(params,currentStoreId);
        Map<String, Object> map = new HashMap<>();
        map.put("list", commentPage.getData());
        map.put("total", commentPage.getTotal());
        return Result.ok(map);
    }

    @ApiOperation(value = "商品评价详情", notes = "商品评价详情")
    @GetMapping("/detail")
    public Result<?> queryGoodsCommentDetail(@RequestParam("commentId") Integer commentId) {
        GoodsCommentVO comment = commentService.queryCommentDetail(commentId);
        Map<String, Object> map = new HashMap<>();
        map.put("detail", comment);
        return Result.ok(map);
    }

    // comment/list
    @ApiOperation(value = "商品评价列表", notes = "商品评价列表")
    @GetMapping("/list")
    public Result<?> queryGoodsCommentPageList(QueryCommentDTO params) {
        Integer currentStoreId = clientInfoUtils.getCurrentStoreId();
        PageResult<GoodsCommentVO> commentPage = commentService.queryCommentList(params, currentStoreId);
        return Result.resultPageList(commentPage);
    }
    // comment/total
    @ApiOperation(value = "商品评价总数", notes = "商品评价总数")
    @GetMapping("/total")
    public Result<?> queryGoodsCommentTotal(@RequestParam("goodsId") Integer goodsId) {
        CommentTotalVO total = commentService.queryCommentTotal(goodsId);
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        return Result.ok(map);
    }

    @ApiOperation(value = "/order/comment/submit", tags = "提交订单评价")
    @PostMapping("/submit")
    public Result<?> submitOrderComment(@RequestBody @Validated(ValidationGroups.Update.class) OrderCommentDTO params){
        Integer userId = SecurityUtil.getCurrentUserIdEx();
        orderService.submitOrderComment(params, userId);
        return Result.ok("提交订单评价成功");
    }

}
